package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.adapter.EmployeeListAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.EmployeeBean;
import com.txsj.scan.presenter.SelectEmployeePresenter;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.view.ISelectEmployeeView;
import com.txsj.scan.widget.TSnackbarView;
import com.txsj.scan.widget.TitleBarView;
import com.txsj.scan.widget.swipemenu.SwipeRecyView;

import java.util.ArrayList;
import java.util.List;

public class SelectEmployeeActivity extends BaseActivity<SelectEmployeePresenter> implements ISelectEmployeeView {

    @Override
    protected SelectEmployeePresenter providePresenter() {
        return new SelectEmployeePresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    private List<EmployeeBean> employeeList;
    EmployeeListAdapter adapter;
    XRefreshView xRefreshView;
    SwipeRecyView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_warehouse);
        initView();
        initData();
    }

    private void initData() {
        this.employeeList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        TitleBarView title = findViewById(R.id.titleBar);
        title.setCenterText("选择员工");
        adapter = new EmployeeListAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setListener(new EmployeeListAdapter.OnItemClickListener() {
            @Override
            public void onCLick(int position) {
                Intent intent = getIntent();
                String json = new Gson().toJson(employeeList.get(position));
                intent.putExtra("result", json);
                setResult(0x185, intent);
                finish();
            }
        });
        initEmptyView();
        xRefreshView.enableEmptyView(false);
        xRefreshView.setPullRefreshEnable(true);
        xRefreshView.setAutoLoadMore(false);
        xRefreshView.startRefresh();
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                presenter.getEmployeeList();
            }
        });
    }


    @Override
    public void loadUserListSuccess(List<EmployeeBean> dataList) {
        if (dataList == null && dataList.size() == 0) {
            return;
        }
        xRefreshView.stopRefresh();
        employeeList.clear();
        employeeList.addAll(dataList);
        adapter.setList(employeeList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadFailed(String errMsg) {
        new TSnackbarView(xRefreshView, errMsg);
        xRefreshView.stopRefresh();
    }

    private void initView() {
        recyclerView = (SwipeRecyView) findViewById(R.id.recyclerView);
        xRefreshView = (XRefreshView) findViewById(R.id.xRefreshView);
    }

    Button btn_empty_reload;

    /**
     * 列表数据为空时 显示的View
     */
    private void initEmptyView() {
        View emptyView = View.inflate(this, R.layout.view_empty, null);
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRefreshView.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xRefreshView.startRefresh();
            }
        });
    }
}
